var prefix = api + "/admin/social";

$(function () {
	var datas = dictByType('social_type')
	renderDictFilter('#type', 'social_type')
	var addFlag = permissions.sys_social_details_add
	var editFlag = permissions.sys_social_details_edit
	var deleteFlag = permissions.sys_social_details_del
	//没有新增权限的话删除新增按钮
	if (!addFlag) {
		$('#addBtn').remove()
	}
	var options = {
		url: prefix + "/page",
		updateUrl: "/pages/sysadm/social/socialEdit",
		removeUrl: prefix + "/{id}",
		sortName: "createTime",
		sortOrder: "desc",
		modalName: "密钥管理",
		columns: [
			{
				title: "序号",
				align: 'center',
				valign: 'middle',
				formatter: function (value, row, index) {
					return $.table.serialNumber(index);
				}
			},
			{
				field: 'type',
				title: '类型',
				align: 'center',
				valign: 'middle',
				formatter: function (value, row, index) {
					// return $.table.selectDictLabel(datas, value);
					return selectDictLabel(datas, value);
				}
			},
			{
				field: 'remark',
				title: '描述',
				align: 'center',
				valign: 'middle',
			},
			{
				field: 'appId',
				title: 'appId',
				align: 'center',
				valign: 'middle',
			},
			{
				field: 'appSecret',
				title: 'appSecret',
				align: 'center',
				valign: 'middle',
			},
			{
				field: 'createTime',
				title: '创建时间',
				valign: 'middle',
				align: 'center',
				sortable: true,
			},
			{
				title: '操作',
				align: 'center',
				width: 200,
				formatter: function (value, row, index) {
					var actions = [];
					if (editFlag) {
						actions.push('<a class="btn btn-success btn-xs " href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
					}
					actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="$.operate.view(\'' + row.id + '\')"><i class="fa fa-search"></i>详细</a>');
					if (deleteFlag) {
						actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
					}
					return actions.join('');
				}
			}]
	};
	$.table.init(options);
});


function selectDictLabel(datas, value) {
	var actions = [];
	$.each(datas, function (index, dict) {
		var listClass = "";
		if (dict.value == ('' + value)) {
			actions.push($.common.sprintf("<span class='%s'>%s</span>", listClass, dict.label));
			return false;
		} else {
			actions.push($.common.sprintf("<span class='%s'>%s</span>", listClass, value));
			return false;
		}
	});
	return actions.join('');
}